﻿using System.Linq;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KZ.Exception;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery.DAL
{
    public class SettingValueDAL
    {
        private static SettingValue CreateSettingValueFromReader(SqlDataReader reader)
        {
            SettingValue settingValue = new SettingValue
            {
                Day = Utility.IntParse(reader[SettingValueColumns.Day.ToString()]),
                Value = Utility.DecimalParse(reader[SettingValueColumns.Value.ToString()])
            };

            return settingValue;
        }

        public static SettingValue GetSettingValueFirstDay()
        {
            try
            {
                SettingValue settingValue = new SettingValue();

                using (SqlDataReader reader = DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.SettingValueGetFirstDay))
                {
                    while (reader.Read())
                    {
                        settingValue = CreateSettingValueFromReader(reader);
                    }
                }

                return settingValue;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetSettingValueFirstDay", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_SETTINGVALUE_GET);
            }
        }

        public static SettingValue GetSettingValueLastDay()
        {
            try
            {
                SettingValue settingValue = new SettingValue();

                using (SqlDataReader reader = DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.SettingValueGetLastDay))
                {
                    while (reader.Read())
                    {
                        settingValue = CreateSettingValueFromReader(reader);
                    }
                }

                return settingValue;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetSettingValueLastDay", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_SETTINGVALUE_GET);
            }
        }

        public static List<SettingValue> GetSettingValueList()
        {
            try
            {
                List<SettingValue> settingValueList = new List<SettingValue>();

                using (SqlDataReader reader = DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.SettingValueGetList))
                {
                    while (reader.Read())
                    {
                        settingValueList.Add(CreateSettingValueFromReader(reader));
                    }
                }

                return settingValueList;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetSettingValueList", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_SETTINGVALUE_GET);
            }
        }

        public static void DeleteSettingValue()
        {
            try
            {
                DataAccessHelper.ExecuteNonQueryWithTransaction(CommandType.StoredProcedure, KZ_StoreName.SettingValueDelete);
            }
            catch (SqlException ex)
            {
                Log.LogEx("DeleteSettingValue", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_SETTINGVALUE_DELETE);
            }
        }

        public static bool UpdateSettingValue(List<SettingValue> settingValueList)
        {
            try
            {
                DataAccessHelper.BeginTransaction(IsolationLevel.Serializable);

                DeleteSettingValue();

                SettingValueDAL settingValueDAL = new SettingValueDAL();
                
                decimal firstDay = settingValueList.Single(sv => sv.Day == 1).Value;
                foreach (SettingValue item in settingValueList)
                {
                    settingValueDAL.InsertSetting(item);
                }

                RuleDetailGroupReportDAL ruleDetailGroupReportDAL = new RuleDetailGroupReportDAL();
                ruleDetailGroupReportDAL.UpdateRuleDetailGroupReportDay();

                RuleDetailGroupDAL ruleDetailGroupDAL = new RuleDetailGroupDAL();
                ruleDetailGroupDAL.UpdateRuleDetailGroupFirstDay();

                DataAccessHelper.CommitTransaction();

                return true;
            }
            catch (DataAccessException)
            {
                DataAccessHelper.RollbackTransaction();
                throw;
            }
        }

        public bool InsertSetting(SettingValue settingValue)
        {
            try
            {
                DataAccessHelper.SqlCmd.Parameters.Add(SettingValueColumns.Day.ToString(), SqlDbType.NVarChar).Value = settingValue.Day;
                DataAccessHelper.SqlCmd.Parameters.Add(SettingValueColumns.Value.ToString(), SqlDbType.NVarChar).Value = settingValue.Value;
                if (DataAccessHelper.ExecuteNonQueryWithTransaction(CommandType.StoredProcedure, KZ_StoreName.SettingValueInsert) > 0)
                {
                    return true;
                }
                throw new DataAccessException(KZ_Message.BLM_ERR_SETTINGVALUE_INSERT);
            }
            catch (SqlException ex)
            {
                Log.LogEx("InsertSetting", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_SETTINGVALUE_INSERT);
            }
        }
    }
}